package com.hanhanxiaochong.zhongxiangchuanmei.view.main.act;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.OrderBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.page.PageRecorder;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.OrderPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseFragment;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IOrderView;
import com.hanhanxiaochong.zhongxiangchuanmei.view.main.adapter.OrderAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.ConfirmDialog;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.LogisticsDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/12.
 * Desc   :
 */

public class OrderFragment extends BaseFragment<OrderPresenter> implements IOrderView {

    @BindView(R.id.points_order_recyclerview)
    RecyclerView recycler;
    @BindView(R.id.smart)
    SmartRefreshLayout smart;

    private PageRecorder pageRecorder = new PageRecorder();
    private OrderAdapter orderAdapter;
    private List<OrderBean> list = new ArrayList<>();
    private String token;
    //1:全部;2:代付款;3:待评价;4:待服务;5:代收货
    private int type;

    public static OrderFragment newInstance(int type) {
        OrderFragment orderFragment = new OrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        orderFragment.setArguments(bundle);
        return orderFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_points_allorder;
    }

    @Override
    protected void init() {
        token = SharedPreferencesUtil.getsInstances(getActivity()).getLoginInfo().getToken();
        Bundle arguments = getArguments();
        if (arguments != null)
            type = arguments.getInt("type", 1);
        orderAdapter = new OrderAdapter(getActivity(), R.layout.item_order, list, type);
        recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycler.setAdapter(orderAdapter);
        orderAdapter.setOnOrderBtnClick(new OrderAdapter.OrderBtnClickListener() {
            @Override
            public void onOrderCancel(String order_num, String order_id, String type) {
                if (type.equals("timeout")) {
                    mPresenter.cancelOrder(token, order_id, order_num);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("content", "要取消该订单么?");
                    ConfirmDialog dialog = ConfirmDialog.newInstance(bundle);
                    dialog.setOnConfirmListener(() -> {
                        mPresenter.cancelOrder(token, order_id, order_num);
                    });
                    dialog.show(getChildFragmentManager(), "ConfirmDialog");
                }
            }

            @Override
            public void onOrderDelete(String order_num, String order_id) {

            }

            @Override
            public void onCheckLogistics(String address, String express_name, String order_id) {
                Bundle bundle = new Bundle();
                bundle.putString("address", address);
                bundle.putString("express_name", express_name);
                bundle.putString("order_id", order_id);
                LogisticsDialog dialog = LogisticsDialog.newInstance(bundle);
                dialog.show(getChildFragmentManager(), "LogisticsDialog");
            }

            @Override
            public void onOrderConfirm(String order_id) {
                Bundle bundle = new Bundle();
                bundle.putString("content", "确认收货后款项将支付给商家?");
                ConfirmDialog dialog = ConfirmDialog.newInstance(bundle);
                dialog.setOnConfirmListener(() -> {
                    mPresenter.changeOrderStatus(token, order_id);
                });
                dialog.show(getChildFragmentManager(), "ConfirmDialog");
            }
        });
    }

    @Override
    protected void initDatas() {
        mPresenter.getOrderList(token, type, pageRecorder.getCurrentPage());
    }

    @Override
    protected void initPresenter() {
        mPresenter = new OrderPresenter(this);
    }

    @Override
    protected void listen() {
        smart.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mPresenter.getOrderList(token, type, pageRecorder.getCurrentPage());
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageRecorder.moveToFirstPage();
                mPresenter.getOrderList(token, type, pageRecorder.getCurrentPage());
            }
        });
    }

    @Override
    public void setOrderData(List<OrderBean> data) {
        smart.finishRefresh();
        pageRecorder.moveToNextPage();
        orderAdapter.setDatas(data);
    }

    @Override
    public void addOrderData(List<OrderBean> data) {
        smart.finishLoadmore();
        pageRecorder.moveToNextPage();
        orderAdapter.addDatas(data);
    }

    @Override
    public void cancelSuccess() {
        ToastUtils.showToast("取消订单成功");
        pageRecorder.moveToFirstPage();
        mPresenter.getOrderList(token, type, pageRecorder.getCurrentPage());
    }

    @Override
    public void changeOrderStatusSuccess() {
        pageRecorder.moveToFirstPage();
        mPresenter.getOrderList(token, type, pageRecorder.getCurrentPage());
    }
}
